package com.saturn.titan.core.repository.mybatis.base.sqlprovider;

import java.lang.reflect.Field;
import java.util.List;

import com.saturn.titan.core.bean.base.PageableBean;
import com.saturn.titan.core.repository.mybatis.annotations.DbField;
import com.saturn.titan.core.repository.mybatis.annotations.DbRangeQueryField;
import com.saturn.titan.core.repository.mybatis.annotations.DbTable;
import com.saturn.titan.core.repository.mybatis.base.BaseDaoSqlProvider;

/**
 * 通用接口的selectCount方法的sql生成器
 * @author lwh
 *
 */
public class BaseDaoSelectCountSqlProvider extends BaseDaoSqlProvider{

	public String doBuildSql(Object param,Class<?> beanType) throws Exception {
		
		DbTable dbTableAnno = getDbTableAnno(beanType);
		
		String tableName = dbTableAnno.table();
		
		List<Field> beanFields = getAllField(beanType);
		
		String tmpColumnName = null;
		Class<?> tmpColumnType = null;
		String sqlWhere = "";
		boolean hasWhere = false;
		Object fieldValue = null;
		
		String groupSqlField = " ";
		
		for(Field f : beanFields){
			DbField field = f.getAnnotation(DbField.class);
			DbRangeQueryField queryField = f.getAnnotation(DbRangeQueryField.class);
			
			//只对标注了为数据库字段的属性进行处理
			if(field == null && queryField == null){
				continue;
			}
			
			if(field != null){
				tmpColumnName = getColumnName(f, field);
				groupSqlField += "`" + tmpColumnName + "`,";
			}else{
				tmpColumnName = queryField.column();
			}
			
			tmpColumnType = getColumnType(f, field);
			
			fieldValue = getColumnValue(param,f,beanType);
			
			if(fieldValue != null){
				hasWhere = true;
				sqlWhere += tmpColumnName + " " + getOperation(queryField) + " " + setupFieldValue(tmpColumnType,fieldValue) + " and ";
			}
			
		}
		String sql = "select count(*) ";
		sql += " from " + tableName ;
		if(hasWhere){
			sql +=" where " + sqlWhere.substring(0,sqlWhere.length() - 4);
		}
		
		if(param instanceof PageableBean){
			PageableBean bean = (PageableBean)param;
			if(hasGroupInfo(bean)){
				
				String groupByColumns = " ";
				for(String col : bean.getGroupBy().getGroupBy()){
					groupByColumns += col+",";
				}
				
				if(bean.getGroupBy().getSumColumn() != null){
					for(String col : bean.getGroupBy().getSumColumn()){
						groupSqlField = groupSqlField.replaceAll("`"+col+"`", "sum("+col+") as "+col);
					}
				}
				
				if(bean.getGroupBy().getMaxColumn() != null){
					for(String col : bean.getGroupBy().getSumColumn()){
						groupSqlField = groupSqlField.replaceAll("`"+col+"`", "max("+col+") as "+col);
					}
				}
				
				if(bean.getGroupBy().getMinColumn() != null){
					for(String col : bean.getGroupBy().getSumColumn()){
						groupSqlField = groupSqlField.replaceAll("`"+col+"`", "min("+col+") as "+col);
					}
				}
				
				if(bean.getGroupBy().getNullColumn() != null){
					for(String col : bean.getGroupBy().getNullColumn()){
						groupSqlField = groupSqlField.replaceAll("`"+col+"`", "'' as "+col);
					}
				}
				
				sql = "select count(*) " +
						" from ( select " +groupSqlField.substring(0,groupSqlField.length() - 1) + 
								" from " + tableName;
				if (hasWhere) {
					sql += " where " + sqlWhere.substring(0, sqlWhere.length() - 4);
				}
				sql += " group by " + groupByColumns.substring(0,groupByColumns.length() - 1) + 
								") as T1";
				
				
			}
		}
		return sql;
	}
	
}
